package com.zygh.lz.stapprove;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.Optional;

/**
 * 省厅入参解析
 */
@Data
@ToString
public class InBean implements Serializable {

    private String messageId;
    private Object parameter;
    private String version;

    public <T> T convertObj( Class<T> cls ){
        String data = JSON.toJSONString( this.parameter );
        JSONObject jsonObj = JSONObject.parseObject( data );

        JSONArray arrayKeyVal = jsonObj.getJSONObject( "condition" ).getJSONArray( "keyValueList" );

        return arrayKeyVal
                .stream()
                .map( JSON::toJSONString )
                .filter( keyVal -> JSONObject.parseObject( keyVal ).getString( "key" ).equals( "json" ) )
                .map( JSONObject::parseObject )
                .map( keyValObj -> JSONObject.parseObject( keyValObj.getString( "value" ), cls ) ).findFirst().orElse( null );
    }
}
